unit Vor2dPickFm;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.ExtCtrls,
  Vcl.StdCtrls,
  GraphObjects,
  GLS.Material,
  GLS.Cadencer,
  GLS.SceneViewer,

  GLS.BaseClasses,
  GLS.Scene,
  GLS.Canvas,
  GLS.RenderContextInfo,
  VoronoiShamos,
  GLS.Coordinates;

type
  TFormVor2dPick = class(TForm)
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Button1: TButton;
    Label1: TLabel;
    GLScene1: TGLScene;
    GLCadencer1: TGLCadencer;
    GLMaterialLibrary1: TGLMaterialLibrary;
    GLDirectOpenGL: TGLDirectOpenGL;
    GLCamera: TGLCamera;
    GLLightSource: TGLLightSource;
    PanelPB: TPanel;
    PaintBox: TPaintBox;
    GLSceneViewer: TGLSceneViewer;
    procedure PaintBoxPaint(Sender: TObject);
    procedure PaintBoxMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBoxMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure GLDirectOpenGLRender(Sender: TObject;
      var rci: TGLRenderContextInfo);
  private
  end;

var
  FormVor2dPick: TFormVor2dPick;

implementation

{$R *.dfm}

var
  List: TList;
  Vor: TVoronoi;
  mx, my, i: Integer;
  Down: Boolean;

procedure TFormVor2dPick.PaintBoxPaint(Sender: TObject);
var
  z: Integer;
begin
  // for Paintbox.Color
  PaintBox.Canvas.FillRect(Rect(0, 0, PanelPB.Width , PanelPB.Height));
//  PaintBox.Canvas.FloodFill(0, 0, Paintbox.Color, fsSurface);
  if List = nil then
    Exit;
  PaintBox.Canvas.Font.Color := clWhite;
  for z := 0 to List.Count - 1 do
  begin
    TGraphObject(List.items[z]).draw;
    if (TObject(List.items[z]) is TGPoint) and CheckBox3.Checked then
      PaintBox.Canvas.TextOut(
        Round(TGPoint(List.items[z]).GetX + 1),
        Round(TGPoint(List.items[z]).GetY + 1), IntToStr(z));
  end;
end;

procedure TFormVor2dPick.PaintBoxMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  mx := X;
  my := Y;
  FormVor2dPick.Caption := IntToStr(mx) + ' ' + IntToStr(my);
  if down then
  begin
    TGPoint(List.items[i]).MoveTo(X, Y);
    CheckBox1Click(FormVor2dPick);
  end;
end;

procedure TFormVor2dPick.FormCreate(Sender: TObject);
begin
  List := TList.Create;
  down := false;
end;

procedure TFormVor2dPick.GLDirectOpenGLRender(Sender: TObject;
  var rci: TGLRenderContextInfo);
var
  i, x, y: Integer;
  GLCanvas: TGLCanvas;
  r: TRect;
  Color: TColor;
begin
  GLCanvas := TGLCanvas.Create(256, 256);
(*
  GLCanvas.PenWidth := vPenWidth;
  case vWhat of
    wLines:
      begin
        for i := 1 to cNbLines do
        begin
          GLCanvas.PenColor := Random(256 * 256 * 256);
          GLCanvas.MoveTo(Random(256), Random(256)); // first point
          GLCanvas.LineTo(Random(256), Random(256)); // second point
        end;
      end;
    wEllipses:
      for i := 1 to cNbEllipses do
      begin
        GLCanvas.PenColor := Random(256 * 256 * 256);
        GLCanvas.EllipseBB(Random(256), Random(256), Random(256), Random(256));
      end;
    wRects:
      for i := 1 to cNbRects do
      begin
        GLCanvas.PenColor := Random(256 * 256 * 256);
        r := Rect(Random(256), Random(256), Random(256), Random(256));
        GLCanvas.FillRect(r.Left, r.Top, r.Right, r.Bottom);
      end;
    wPoints:
      begin
        for i := 1 to cNbPoints do
        begin
          GLCanvas.PenColor := Random(256 * 256 * 256);
          GLCanvas.PlotPixel(Random(256), Random(256));
        end;
      end;
    wTextOut:
      begin
        for i := 1 to cNbTextOuts do
        begin
          Color := Random(256 * 256 * 256);
          x := Random(256);
          y := Random(256);
          WindowsBitmapFont.TextOut(rci, x, y, 'Hello', Color);
        end;
      end;
    wArcs:
      begin
        for i := 1 to cNbEllipses do
        begin
          GLCanvas.PenColor := Random(256 * 256 * 256);
          GLCanvas.Arc(Random(256), Random(256), Random(256), Random(256),
            Random(256), Random(256), Random(256), Random(256))
        end;
      end;
  end;
*)
  GLCanvas.Free;
end;

procedure TFormVor2dPick.Button1Click(Sender: TObject);
begin
  List.Clear;
  PaintBox.OnPaint(FormVor2dPick);
end;

procedure TFormVor2dPick.CheckBox1Click(Sender: TObject);
begin
  Vor := TVoronoi.Create(PaintBox.Canvas, List);
  Vor.ClearLines;
  Vor.CalcVoronoi(CheckBox1.Checked, CheckBox2.Checked);
  Vor.Free;
  PaintBox.OnPaint(FormVor2dPick);

end;

procedure TFormVor2dPick.PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  p: TGPoint;
  z: Integer;
begin
  p := TGPoint.Create(X, Y, nil, nil);
  p.closeDist := 5;
  for z := 0 to List.Count - 1 do
  begin
    if TObject(List.items[z]) is TGPoint then
      if p.Match(TGPoint(List.Items[z])) then
      begin
        if (Button = TMouseButton.mbRight) then
          TGPoint(List.Items[z]).Delete(true)
        else
        begin
          down := True;
          i := z;
        end;
        CheckBox1Click(FormVor2dPick);
        exit;
      end;
  end;
  p.Free;

  TGPoint.Create(X, Y, List, PaintBox.Canvas);
  CheckBox1Click(FormVor2dPick);
end;

procedure TFormVor2dPick.PaintBoxMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  down := false;
end;

end.
